package com.example.service;

import com.baidu.aip.speech.AipSpeech;
import com.example.utils.VoiceUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;

@Service
public class VoiceServiceImpl  {

    private final String APP_ID = "115632038";
    private final String API_KEY = "uzmhjlZCTVYTiOHr02UooiyC";
    private final String SECRET_KEY = "bVQXvbwkr7zavgh8qaCwkrBj4dbtlwjS";


    public String speechRecognite(String fileName){
        AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
        client.setConnectionTimeoutInMillis(20000);
        client.setSocketTimeoutInMillis(60000);
        try {
            byte[] pcmData = VoiceUtils.mp3Convert2pcm(new FileInputStream(fileName));
            JSONObject result = client.asr(pcmData, "pcm", 16000, null);
            return result.toString(2);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
